package com.kai.rpc.provider.net;

import com.kai.rpc.provider.dispatch.Dispatch;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author kai
 * @date 2021/3/20 8:44
 * @description:
 */
public class RPCProcess implements Runnable {

    Socket socket;

    public RPCProcess(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        ObjectOutputStream outputStream = null;

        ObjectInputStream inputStream = null;
        try {
            inputStream = new ObjectInputStream(socket.getInputStream());

            Object object = inputStream.readObject();

            Object respObj = Dispatch.dispatch(object);


            outputStream = new ObjectOutputStream(socket.getOutputStream());

            outputStream.writeObject(respObj);

            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
